﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;

namespace EntityServices
{
    /// <summary>
    /// Summary description for PhieuDieuChinhDiem
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PhieuDieuChinhDiem : System.Web.Services.WebService
    {
        [WebMethod]
        public bool GhiNhanPhieuDieuChinhDiem(string maGiaoVien, string maLopHoc, string hinhThucDieuChinhDiem, DateTime ngayChinh, List<SINH_VIEN> dsSinhVien, List<int> dsDiemCapNhat)
        {
            try
            {
                HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();

                int giaoVienID = 0;
                foreach (GIAO_VIEN gv in db.GIAO_VIENs)
                {
                    if (gv.MASOGIAOVIEN == maGiaoVien)
                    {
                        giaoVienID = gv.GIAOVIENID;
                    }
                }

                int lopHocID = 0;
                foreach (LOP_HOC lh in db.LOP_HOCs)
                {
                    if (lh.MALOPHOC == maLopHoc)
                    {
                        lopHocID = lh.LOPHOCID;
                    }
                }

                PHIEU_DIEU_CHINH_DIEM p = new PHIEU_DIEU_CHINH_DIEM { GIAOVIENID = giaoVienID, LOPHOCID = lopHocID, HINHTHUCDIEUCHINH = hinhThucDieuChinhDiem, NGAYCHINH = ngayChinh };
                db.PHIEU_DIEU_CHINH_DIEMs.InsertOnSubmit(p);
                db.SubmitChanges();

                int count = 0;
                count = db.PHIEU_DIEU_CHINH_DIEMs.Count();
                int phieuDCDiemID = db.PHIEU_DIEU_CHINH_DIEMs.ElementAt(count).PHIEUDIEUCHINHDIEMID;

                count = dsSinhVien.Count();
                for (int i = 0; i < count; i++)
                {
                    CT_PHIEUDIEUCHINHDIEM c = new CT_PHIEUDIEUCHINHDIEM { PHIEUDIEUCHINHDIEMID = phieuDCDiemID, SINHVIENID = dsSinhVien[i].SINHVIENID, DIEMCAPNHAT = dsDiemCapNhat[i] };
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        [WebMethod]
        public List<CT_PHIEUDIEUCHINHDIEM> LayPhieuDieuChinhDiem(string maLopHoc, string hinhThucDieuChinh)
        {
            List<CT_PHIEUDIEUCHINHDIEM> ds = new List<CT_PHIEUDIEUCHINHDIEM>();
            HTQLGiaoDucDataContext db = new HTQLGiaoDucDataContext();

            int lopHocID = 0;
            foreach (LOP_HOC lh in db.LOP_HOCs)
            {
                if (lh.MALOPHOC == maLopHoc)
                {
                    lopHocID = lh.LOPHOCID;
                }
            }
            int phieuDCDiemID = 0;
            foreach (PHIEU_DIEU_CHINH_DIEM p in db.PHIEU_DIEU_CHINH_DIEMs)
            {
                if (p.LOPHOCID == lopHocID && p.HINHTHUCDIEUCHINH == hinhThucDieuChinh)
                {
                    phieuDCDiemID = p.PHIEUDIEUCHINHDIEMID;
                }
            }
            var ctPhieus = db.CT_PHIEUDIEUCHINHDIEMs.Where(temp => temp.PHIEUDIEUCHINHDIEMID.Equals(phieuDCDiemID));
            foreach (CT_PHIEUDIEUCHINHDIEM c in ctPhieus)
            {
                CT_PHIEUDIEUCHINHDIEM ct = new CT_PHIEUDIEUCHINHDIEM { CTPDCDIEMID = c.CTPDCDIEMID, PHIEUDIEUCHINHDIEMID = c.PHIEUDIEUCHINHDIEMID, SINHVIENID = c.SINHVIENID, DIEMCAPNHAT = c.DIEMCAPNHAT};
                ds.Add(ct);
            }
            return ds;
        }
    }
}
